<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

class Analysis extends Backend
{
    public $navTitle = "analysis";

    public function user()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');

            if ($action == 'pie') {
                $sex0 = model('User.User')->where('sex', 0)->count();
                $sex1 = model('User.User')->where('sex', 1)->count();
                $sex2 = model('User.User')->where('sex', 2)->count();

                $auth0 = model('User.User')->where('auth', 0)->count();
                $auth1 = model('User.User')->where('auth', 1)->count();

                $data = [
                    'sex' => [$sex0, $sex1, $sex2],
                    'auth' => [$auth0, $auth1]
                ];

                $this->success('用户统计', null, $data);
            }
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle
        ]);
    }

    // 帖子分析
    public function post()
    {
        $nav_title = [
            'nav_title' => '数据统计',
            'title' => '问答统计'
        ];

        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '', 'trim');

            //线性图数据
            if ($action == 'line') {
                $catelist = model('Post.Cate')->field('id,name')->select();
                //提取名字
                $name = array_column($catelist, 'name');

                //提取id数据
                $ids = array_column($catelist, 'id');
                // halt($ids);

                //已采纳 /未采纳
                $Accept = $NoAccept = [];

                foreach ($ids as $cateid) {
                    //已采纳
                    $Accept[] = model('Post.Post')
                        ->where('accept', 'EXP', Db::raw('IS NOT NULL'))
                        ->where('cateid', '=', $cateid)
                        ->count();

                    //未采纳
                    $NoAccept[] = model('Post.Post')
                        ->where('accept', 'EXP', Db::raw('IS NULL'))
                        ->where('cateid', '=', $cateid)
                        ->count();
                }

                //组装数据
                $data = [
                    'name' => $name,
                    'Accept' => $Accept,
                    'NoAccept' => $NoAccept
                ];

                // halt($data);

                $this->success('返回数据成功', null, $data);
                exit;
            }
        }

        return $this->fetch('', [
            'nav_title' => $nav_title,
            'navTitle' => $this->navTitle
        ]);
    }

    // 消费记录统计
    public function record()
    {
        $nav_title = [
            'nav_title' => '数据统计',
            'title' => '消费记录统计'
        ];

        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '', 'trim');

            // halt($action);
            //饼图
            if ($action == 'record') {
                //获取当前年份
                $year = date("Y", time());

                $TimeList = [];

                for ($i = 1; $i <= 12; $i++) {
                    $start = "$year-$i-01";
                    $end = date("Y-m-d", strtotime("$start +1 month -1 day"));
                    $TimeList[] = [$start, $end];
                }

                $state1 = ['发布'];
                $state2 = ['采纳'];
                $state3 = ['充值'];
                $state4 = ['签到'];
                $state5 = ['商品订单'];
                $state6 = ['预约订单'];
                $state7 = ['退款'];
                foreach ($TimeList as $item) {
                    $state1[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '1'])->count();
                    $state2[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '2'])->count();
                    $state3[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '3'])->count();
                    $state4[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '4'])->count();
                    $state5[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '5'])->count();
                    $state6[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '6'])->count();
                    $state7[] = model('User.Record')->whereTime('createtime', 'between', $item)->where(['state' => '-1'])->count();
                }

                //X轴
                $month = ['record'];
                for ($i = 1; $i <= 12; $i++) {
                    $month[] = "{$i}月份";
                }

                //组装数据
                $data = [
                    $month,
                    $state1,
                    $state2,
                    $state3,
                    $state4,
                    $state5,
                    $state6,
                    $state7,
                ];


                $this->success('消费记录统计', null, $data);
                exit;
            }
        }
        return $this->fetch('', [
            'nav_title' => $nav_title,
            'navTitle' => $this->navTitle

        ]);
    }
}
